home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Atlanta_1990 / Atlanta-Devcon.1 / Libraries / Intuition / boopsi / emboxclass.save < prev    next >
Encoding:
Text File  |  1992-08-26  |  6.0 KB  |  275 lines

  1. /* emboxclass.c -- :ts=8
  2.  * Example of a private image class.
  3.  * This one does a raised embossed box.
  4.  * Doesn't do resolution sensitive bevel widths yet,
  5.  * nor respect IA_LINEWIDTH.
  6.  */
  7.  
  8. /*
  9. Copyright (c) 1989, 1990 Commodore-Amiga, Inc.
  10.  
  11. Executables based on this information may be used in software
  12. for Commodore Amiga computers. All other rights reserved.
  13. This information is provided "as is"; no warranties are made.
  14. All use is at your own risk, and no liability or responsibility
  15. is assumed.
  16. */
  17.  
  18. #include "sysall.h"
  19.  
  20. #if 0    /* in sysall.h    */
  21. #include <intuition/classusr.h>
  22. #include <intuition/classes.h>
  23. #include <intuition/imageclass.h>
  24. #endif
  25.  
  26. #include <intuition/classes.h>
  27.  
  28. #include <graphics/gfxmacros.h>
  29.  
  30. #define D(x)    ;
  31. #define DH(x)    ;
  32.  
  33. struct EmbBData {
  34.     /* right now, this is kept in pixels, and isn't adjusted
  35.      * for resolution.  A fancier version might take this
  36.      * parameter in "resolution ticks"
  37.      */
  38.     UWORD        ebd_LineWidth;
  39. };
  40.  
  41. #define IM(o)    ((struct Image *)(o))    /* transparent base class */
  42.  
  43. #define DEFAULTLINEWIDTH    1
  44.  
  45. /* private class    */
  46. #define PRIVATECLASS    TRUE
  47.  
  48. #if PRIVATECLASS
  49. #define MYCLASSID    (NULL)
  50. extern struct Library    *IntuitionBase;
  51. #endif
  52.  
  53. #define SUPERCLASSID    (IMAGECLASS)
  54.  
  55. Class    *
  56. initEmbBClass()
  57. {
  58.     ULONG    dispatchEmbB();
  59.     ULONG    hookEntry();
  60.     Class    *cl;
  61.     Class    *MakeClass();
  62.  
  63.     if ( cl =  MakeClass( MYCLASSID, 
  64.         SUPERCLASSID, NULL,        /* superclass is public      */
  65.          sizeof (struct EmbBData) ) )    /* my object's instance data */
  66.     {
  67.     /* initialize the cl_Dispatcher Hook    */
  68.     cl->cl_Dispatcher.h_Entry = hookEntry;
  69.     cl->cl_Dispatcher.h_SubEntry = dispatchEmbB;
  70.     cl->cl_Dispatcher.h_Data = (VOID *) 0xFACE;    /* unused */
  71.  
  72. #if !PRIVATECLASS
  73.     AddClass( cl );            /* make public and available    */
  74. #endif
  75.     }
  76.     return ( cl );
  77. }
  78.  
  79. freeEmbBClass( cl )
  80. Class    *cl;
  81. {
  82.     return ( FreeClass( cl )  );
  83. }
  84.  
  85. ULONG 
  86. dispatchEmbB( cl, o, msg )
  87. Class   *cl;
  88. Object  *o;
  89. Msg     msg;
  90. {
  91.     Object          *newobj;
  92.     struct EmbBData     *ebd;
  93.  
  94.     switch ( msg->MethodID )
  95.     {
  96.     case OM_NEW:
  97.     /* let superclass do it's creation routine first    */
  98.     if ( newobj = (Object *) DSM( cl, o, msg ) )
  99.     {
  100.         /* init my instance data    */
  101.  
  102.         /* get pointer to it, like a good fellow    */
  103.         ebd = INST_DATA( cl, newobj );
  104.  
  105.         /* mandatory default init */
  106.         ebd->ebd_LineWidth = DEFAULTLINEWIDTH;
  107.  
  108.         /* may override ebd_LineWidth    */
  109.         setEmbBAttrs( cl, newobj, msg );
  110.     }
  111.  
  112.     return ( (ULONG) newobj );
  113.  
  114.     case OM_GET:
  115.     return ( (ULONG) getEmbBAttrs( cl, o, msg  ) );
  116.  
  117.     case OM_SET:
  118.     DSM( cl, o, msg );        /* let the superclass see the atts */
  119.     setEmbBAttrs( cl, o, msg  );    /* set the ones I care about       */
  120.     return ( (ULONG) 1 );        /* i'm happy               */
  121.  
  122.     case IM_DRAW:            /* draw with state */
  123.     case IM_FRAME:            /* special case of draw    */
  124.     return ( (ULONG) drawEmbB( cl, o, msg ) );
  125.  
  126.     /* use superclass defaults */
  127.     case IM_HITTEST:
  128.     case IM_ERASE:
  129.     case OM_DISPOSE:
  130.     default:
  131.     return ( (ULONG) DSM( cl, o, msg ) );
  132.     }
  133. }
  134.  
  135. drawEmbB( cl, o, msg )
  136. Class        *cl;
  137. Object        *o;
  138. struct impDraw    *msg;
  139. {
  140.     struct EmbBData     *ebd = INST_DATA( cl, o );
  141.     struct IBox        box;
  142.  
  143.     UWORD        *pens;        /* pen spec array */
  144.     UWORD        ulpen;        /* upper left    */
  145.     UWORD        lrpen;        /* lower right    */
  146.     UWORD        fillpen;    /* filled area    */
  147.  
  148.     /* let's be sure that we were passed a DrawInfo    */
  149.     pens =  ( msg->imp_DrInfo )?  msg->imp_DrInfo->dri_Pens: NULL;
  150.  
  151.     box = *IM_BOX( IM(o) );        /* get Image.Left/Top/Width/Height */
  152.     box.Left += msg->imp_Offset.X;
  153.     box.Top += msg->imp_Offset.Y;
  154.  
  155.     switch ( msg->imp_State )
  156.     {
  157.     case IDS_SELECTED:
  158.     ulpen = pens? pens[ shadowPen ]: 2;
  159.     lrpen = pens? pens[ shinePen ]: 1;
  160.     fillpen = pens? pens[ hifillPen ]: 3;
  161.     break;
  162.  
  163.     case IDS_NORMAL:    /* doesn't use activefill in borders now */
  164.     case IDS_DISABLED:    /* I don't have a ghosted version yet     */
  165.     case IDS_INDETERMINANT:
  166.     default:
  167.     ulpen = pens? pens[ shinePen ]: 2;
  168.     lrpen = pens? pens[ shadowPen ]: 1;
  169.     fillpen = pens? pens[ backgroundPen ]: 0;
  170.     break;
  171.     }
  172.  
  173.     embossedBoxTrim( msg->imp_RPort, &box,
  174.     ebd->ebd_LineWidth, ebd->ebd_LineWidth,
  175.     ulpen, lrpen );
  176.  
  177.     /* interior */
  178.     interiorBox( msg->imp_RPort, &box,
  179.     ebd->ebd_LineWidth,    /* inset in x dim */
  180.     ebd->ebd_LineWidth,    /* inset in y dim */
  181.     fillpen );
  182. }
  183.  
  184. /* return requested attribute value    */
  185. getEmbBAttrs( cl, o, msg )
  186. Class        *cl;
  187. Object        *o;
  188. struct opGet    *msg;
  189. {
  190.     struct EmbBData     *ebd = INST_DATA( cl, o );
  191.  
  192.     if ( msg->opg_AttrID == IA_LINEWIDTH )
  193.     {
  194.         *msg->opg_Storage = ebd->ebd_LineWidth;
  195.     return ( 1 );
  196.     }
  197.     else
  198.         return ( DSM( cl, o, msg ) );
  199.  
  200. }
  201.  
  202. /* set specified attribute value.
  203.  * Technique provided is a little more general than we need ...
  204.  */
  205. setEmbBAttrs( cl, o, msg )
  206. Class        *cl;
  207. Object        *o;
  208. struct opSet    *msg;
  209. {
  210.     struct TagItem    *NextTagItem();
  211.     struct TagItem    *tags = msg->ops_AttrList;
  212.     struct TagItem    *tag;
  213.     ULONG        tidata;
  214.  
  215.     struct EmbBData     *ebd = INST_DATA( cl, o );
  216.  
  217.     while ( tag = NextTagItem( &tags ) )
  218.     {
  219.     tidata = tag->ti_Data;
  220.     switch ( tag->ti_Tag )
  221.     {
  222.     case IA_LINEWIDTH:
  223.         ebd->ebd_LineWidth = tidata;
  224.         break;
  225.     }
  226.     }
  227. }
  228.  
  229. /* fill region centered in a box */
  230. interiorBox( rp, b, xw, yw, pen )
  231. struct RastPort    *rp;
  232. struct IBox    *b;
  233. {
  234.     if ( (b->Width > (xw<<1)) && (b->Height > (yw<<1)) )
  235.     {
  236.     rp->Mask = -1;
  237.     BNDRYOFF( rp );
  238.     SetAfPt(rp, NULL, 0);
  239.     SetDrMd(rp, JAM2);
  240.     SetAPen(rp, pen);
  241.     RectFill( rp,
  242.         b->Left + xw,
  243.         b->Top + yw,
  244.         b->Left + b->Width - 1 - xw,
  245.         b->Top + b->Height - 1 - yw );
  246.     }
  247. }
  248.  
  249. /* suggestion of Talin: don't draw upper-right and lower-left points */
  250. /* ignoring thickness for now    */
  251. embossedBoxTrim( rp, b, hthick, vthick, ulpen, lrpen )
  252. struct RastPort    *rp;
  253. struct IBox    *b;
  254. {
  255.     int    bottom, right;
  256.  
  257.     bottom = b->Top + b->Height - 1;
  258.     right = b->Left + b->Width - 1;
  259.  
  260.     /* upper right edges    */
  261.     SetAPen( rp, ulpen );
  262.  
  263.     Move( rp, b->Left, bottom - 1 );
  264.     Draw( rp, b->Left, b->Top );
  265.     Draw( rp, right - 1, b->Top );
  266.  
  267.     /* lower right edges    */
  268.     SetAPen( rp, lrpen );
  269.  
  270.     Move( rp, right, b->Top + 1 );
  271.     Draw( rp, right, bottom );
  272.     Draw( rp, b->Left + 1, bottom );
  273. }
  274.  
  275.